﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Komunikator.Web;

namespace Komunikator.Controls
{
    public partial class ChatWindow : FloatableWindow
    {
        Contacts contact;
        Conversations conversation;

        public ChatWindow()
        {
            InitializeComponent();
            this.contact = (Contacts)this.DataContext;
        }

        private void bold_Click(object sender, RoutedEventArgs e)
        {
            if (!(rtbMessage.Selection.GetPropertyValue(Run.FontWeightProperty) is FontWeight) || (FontWeight)rtbMessage.Selection.GetPropertyValue(Run.FontWeightProperty) != FontWeights.Bold)
            {
                rtbMessage.Selection.ApplyPropertyValue(Run.FontWeightProperty, FontWeights.Bold);
            }
            else
            {
                rtbMessage.Selection.ApplyPropertyValue(Run.FontWeightProperty, FontWeights.Normal);
            }
            rtbMessage.Focus();
        }

        private void italic_Click(object sender, RoutedEventArgs e)
        {
            if (!(rtbMessage.Selection.GetPropertyValue(Run.FontStyleProperty) is FontStyle) || (FontStyle)rtbMessage.Selection.GetPropertyValue(Run.FontStyleProperty) != FontStyles.Italic)
            {
                rtbMessage.Selection.ApplyPropertyValue(Run.FontStyleProperty, FontStyles.Italic);
            }
            else
            {
                rtbMessage.Selection.ApplyPropertyValue(Run.FontStyleProperty, FontStyles.Normal);
            }
            rtbMessage.Focus();
        }

        private void underline_Click(object sender, RoutedEventArgs e)
        {
            if (!(rtbMessage.Selection.GetPropertyValue(Run.TextDecorationsProperty) is TextDecorationCollection) || (TextDecorationCollection)rtbMessage.Selection.GetPropertyValue(Run.TextDecorationsProperty) != TextDecorations.Underline)
            {
                rtbMessage.Selection.ApplyPropertyValue(Run.TextDecorationsProperty, TextDecorations.Underline);
            }
            else
            {
                rtbMessage.Selection.ApplyPropertyValue(Run.TextDecorationsProperty, null);
            }
            rtbMessage.Focus();
        }

        private void rtbMessage_SelectionChanged(object sender, RoutedEventArgs e)
        {
            if(rtbMessage.Selection.GetPropertyValue(Run.FontWeightProperty) is FontWeight)
            {
                bold.IsChecked = ((FontWeight)rtbMessage.Selection.GetPropertyValue(Run.FontWeightProperty) == FontWeights.Bold);
            }

            if (rtbMessage.Selection.GetPropertyValue(Run.FontStyleProperty) is FontStyle)
            {
                italic.IsChecked = ((FontStyle)rtbMessage.Selection.GetPropertyValue(Run.FontStyleProperty) == FontStyles.Italic);
            }

            if (rtbMessage.Selection.GetPropertyValue(Run.TextDecorationsProperty) is TextDecorationCollection)
            {
                underline.IsChecked = ((TextDecorationCollection)rtbMessage.Selection.GetPropertyValue(Run.TextDecorationsProperty) == TextDecorations.Underline);
            }
        }
    }
}
